package homework;

import java.util.concurrent.TimeUnit;

/**
 * @author mubei
 * @date 2021/11/29
 * @description
 */
public class Demo04 {
    static volatile boolean flag = true;
    static volatile boolean flag1 = true;
    public static void main(String[] args) throws InterruptedException {

        new Thread(()->{
            while(flag){
                try {
                    System.out.println(Thread.currentThread().getName()+"线程运行  休息5s");
                    TimeUnit.SECONDS.sleep(1);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+"主线程结束");
            flag1 = false;
        },"p1").start();



        new Thread(()->{
            while(flag && flag1){
                try {
                    System.out.println(Thread.currentThread().getName()+"线程运行  休息5s");
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+"主线程结束");
        },"p2").start();


        TimeUnit.SECONDS.sleep(10);
        System.out.println("主线程调用kill");
        flag = false;
    }

}
